home *** CD-ROM | disk | FTP | other *** search
/ Merciful 5 / Merciful - Disc 5.iso / software / p / pcqpascalv1.2d.lha / Examples / CRC.p < prev    next >
Encoding:
Text File  |  1991-07-26  |  1.8 KB  |  84 lines

  1. Program CRC;
  2.  
  3. {
  4.     CRC
  5.  
  6.     This program shows three different methods for calculating
  7.     16-bit CRC values.  The one from the CRC16.i file is based on
  8.     the ZMODEM method, whereas the other two are used for Kermit
  9.     (and XMODEM, I think).  Unfortunately the two methods produce
  10.     different results, although the two Kermit versions are
  11.     compatible.
  12.  
  13.     The ZMODEM method uses a 256-entry (512 byte) table to speed
  14.     up the calculation, and it is the fastest of these three.
  15.     The CRC2 method is second, and the CRC3 method is the slowest
  16.     by far.
  17. }
  18.  
  19. {$I "Include:Utils/Parameters.i"}
  20. {$I "Include:Utils/CRC16.i"}
  21. {$I "Include:Utils/StringLib.i"}
  22.  
  23. var
  24.     FileName    : String;
  25.     InFile    : File of Byte;
  26.  
  27.     CRC1,
  28.     CRC2,
  29.     CRC3    : Word;
  30.  
  31.     c        : Byte;
  32.  
  33. { This CRC algorithm is from the Kermit manual }
  34.  
  35. Function CalcCRC2(c : Byte; crc : Word) : Word;
  36. begin
  37.     crc := (crc shr 4) xor (((crc xor c) and 15) * 4225);
  38.     CalcCRC2 := (crc shr 4) xor (((crc xor (c shr 4)) and 15) * 4225);
  39. end;
  40.  
  41. { This CRC algorithm is from an ancient Kermit program }
  42.  
  43. Function CalcCRC3(c : Byte; crc : Word) : Word;
  44. var
  45.     i : Integer;
  46.     Temp : Integer;
  47. begin
  48.     for i := 0 to 7 do begin
  49.     Temp := crc xor c;
  50.     crc := crc shr 1;
  51.     if Odd(Temp) then
  52.         crc := crc xor $8408;
  53.     c := c shr 1;
  54.     end;
  55.     CalcCRC3 := crc;
  56. end;
  57.  
  58. begin
  59.     FileName := AllocString(256);
  60.     GetParam(1,FileName);
  61.     if FileName^ = '\0' then begin
  62.     Writeln('Usage: CRC filename');
  63.     Exit(10);
  64.     end;
  65.  
  66.     if reopen(FileName, InFile, 2048) then begin
  67.     CRC1 := 0;
  68.     CRC2 := 0;
  69.     CRC3 := 0;
  70.  
  71.     while not eof(InFile) do begin
  72.         Read(InFile, c);
  73.  
  74.         CRC1 := UpdCRC(c, CRC1);
  75.         CRC2 := CalcCRC2(c, CRC2);
  76.         CRC3 := CalcCRC3(c, CRC3);
  77.  
  78.     end;
  79.     Writeln('CRC is ', CRC1, ' ', CRC2, ' ', CRC3);
  80.     end else
  81.     Writeln('Could not open ', FileName);
  82. end.
  83.  
  84.